#!/usr/bin/ruby

$max=50
$interval = 0.03

def print_array( a )
  system "clear"
  $max.downto(1) {
    |line|
    for v in a do
      print v>=line ? '|' : ' '
    end
    print "\n"
  }
  sleep $interval
end

def swap( array, a, b )
  t = array[a]
  array[a] = array[b]
  array[b] = t
  print_array( array )
end

def partition( array, left, right, pivot )
  val = array[pivot]
  swap( array, pivot, right )
  store = left
  for i in left..right-1 do
    if array[i] <= val then
      swap( array, i, store )
      store = store + 1
    end
  end
  swap( array, right, store )
  return store
end
      
def quicksort( array, left, right )
  if right > left then
    index = partition( array, left, right, left )
    quicksort( array, left, index-1 )
    quicksort( array, index+1, right )
  end
end

a = Array.new()
for i in 1..$max do 
  a << (rand*$max).to_i
end

quicksort( a, 0, a.size()-1)


